#!/bin/bash
#
# Dropbox Uploader Script v0.3
#
# by Andrea Fabrizi - andrea.fabrizi@gmail.com

#
# set -x

#DROPBOX ACCOUNT
LOGIN_EMAIL="butaga@gmail.com"
LOGIN_PASSWD="afa17NEdro"

LOGIN_URL="https://www.dropbox.com/login"
HOME_URL="https://www.dropbox.com/home"
UPLOAD_URL="https://dl-web.dropbox.com/upload"
COOKIE_FILE="/tmp/du_cookie_$RANDOM"
RESPONSE_FILE="/tmp/du_resp_$RANDOM"
BIN_DEPS="curl sed grep"
VERSION="0.3"

#Remove temporary files
function remove_temp_files
{
    rm -fr $COOKIE_FILE
    rm -fr $RESPONSE_FILE
}

#Extract token from the specified form
# $1 = file path
# $2 = form action
function get_token
{
    TOKEN=$(cat $1 | tr -s '\n' ' ' | sed -n -e 's/.*<form action="'$2'"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/p')
    echo $TOKEN      
}

#CHECK DEPENDENCIES
for i in $BIN_DEPS; do
    which $i > /dev/null
    if [ $? -ne 0 ]; then
        echo -e "Error: Required file could not be found: $i"
        remove_temp_files
        exit 1
    fi
done

#CHECK PARAMETERS
if [ $# != 2 ]; then
    echo -e " Dropbox Uploader Script v$VERSION"
    echo -e " by Andrea Fabrizi - andrea.fabrizi@gmail.com"
    echo -e "\n Usage:\t $0 [LOCAL_FILE] [REMOTE_FOLDER]\n"
    echo -e " Example:\n\t $0 /etc/myfile.txt /\n"
    remove_temp_files
    exit 1
fi

#CHECK FILE
if [ ! -f $1 ]; then
    echo -e "Error: $1: No such file or directory"
    remove_temp_files
    exit 1
fi

UPLOAD_FILE=$1
DEST_FOLDER=$2

echo -e " Dropbox Uploader Script v$VERSION\n"

#LOAD LOGIN PAGE
echo -ne " > Loading Login Page..."
curl -s -i -o $RESPONSE_FILE "$LOGIN_URL"

if [ $? -ne 0 ]; then
    echo -e " Failed!"
    remove_temp_files
    exit 1
else
    echo -e " OK"
fi

#GET TOKEN
TOKEN=$(get_token "$RESPONSE_FILE" "\/login")
#echo -e " > Token = $TOKEN"
if [ "$TOKEN" == "" ]; then
    echo -e " Failed to get Authentication token!"
    remove_temp_files
    exit 1
fi

#LOGIN
echo -ne " > Login..."
curl -s -i -c $COOKIE_FILE -o $RESPONSE_FILE --data "login_email=$LOGIN_EMAIL&login_password=$LOGIN_PASSWD&t=$TOKEN" "$LOGIN_URL"
grep "location: /home" $RESPONSE_FILE > /dev/null

if [ $? -ne 0 ]; then
    echo -e " Failed!"
    remove_temp_files
    exit 1
else
    echo -e " OK"
fi

#LOAD HOME
echo -ne " > Loading Home..."
curl -s -i -b $COOKIE_FILE -o $RESPONSE_FILE "$HOME_URL"

if [ $? -ne 0 ]; then
    echo -e " Failed!"
    remove_temp_files
    exit 1
else
    echo -e " OK"
fi

#GET TOKEN
TOKEN=$(get_token "$RESPONSE_FILE" "https:\/\/dl-web.dropbox.com\/upload")
#echo -e " > Token = $TOKEN"
if [ "$TOKEN" == "" ]; then
    echo -e " Failed to get Upload token!"
    remove_temp_files
    exit 1
fi

#UPLOAD
echo -ne " > Uploading file..."
curl -s -i -b $COOKIE_FILE -o $RESPONSE_FILE -F "plain=yes" -F "dest=$DEST_FOLDER" -F "t=$TOKEN" -F "file=@$UPLOAD_FILE"  "$UPLOAD_URL"
grep "HTTP/1.1 302 FOUND" $RESPONSE_FILE > /dev/null

if [ $? -ne 0 ]; then
    echo -e " Failed!"
    remove_temp_files
    exit 1
else
    echo -e " OK"
fi

remove_temp_files
